﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
using ComLib.ConfigClass;
using System.IO;
using ComLib.ComType;
using System.Reflection;
using KXmlUIControl;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
//using KWindow.KBase;

namespace WorkFlowConfigManager
{
    public partial class FormWorkFlowSetting : MetroForm
    {
        private string[] Apps = { "IntellPhotoCapturer", "WXSQ", "YSL", "IPC_WXSQ", "IPC_YSL", "HomeApp" };
       
        private WorkFlowOption _option = new WorkFlowOption();

        public FormWorkFlowSetting()
        {
            InitializeComponent();
        }

        private void LoadConfig()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (metroComboApplyType.SelectedIndex == -1)
            {
                MessageBox.Show("未选择有效的业务流程.");
                return;
            }
            _option.ReceiveWay = metroToggleReceiveWay.Checked;
            _option.CapFinger = metroToggleCapFinger.Checked;
            _option.Signature = metroToggleSignature.Checked;
            _option.HightCapture = metroToggleHighCapture.Checked;
            _option.ApplyType = (UserApplyAction)metroComboApplyType.SelectedIndex;
            _option.Save();
            if(SetDefaultApp())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
                MessageBox.Show("设置保存成功!");
            }           
        }

        private bool SetDefaultApp()
        {
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);
            string xmlFile = string.Format(@"{0}\\config\\MainForm.xml", directory);
            if (!File.Exists(xmlFile))
            {
                MessageBox.Show("未找到配置文件" + xmlFile);
                return false;
            }
            XmlConfigApplication configFile = new XmlConfigApplication(xmlFile);
            string defaultAppName = Apps[metroComboApplyType.SelectedIndex];
            if(!configFile.SetDefaultAppName(defaultAppName))
            {
                MessageBox.Show(configFile.Error);
                return false;
            }
            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void FormWorkFlowSetting_Load(object sender, EventArgs e)
        {
            LoadConfig();
            int selIndex = (int)_option.ApplyType;
            metroComboApplyType.SelectedIndex = selIndex;
            metroToggleSignature.Checked = _option.Signature;
            metroToggleCapFinger.Checked = _option.CapFinger;
            metroToggleReceiveWay.Checked = _option.ReceiveWay;
            metroToggleHighCapture.Checked = _option.HightCapture;
        }


    }
}
